using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Controls;

namespace WitCustomControls
{
	public partial class WorkItemPicker : Form
	{
		const string c_parentWorkItemFieldName = "ParentWorkItem";

		private PickWorkItemsControl m_pickControl;
		private WorkItem m_workItem;
		private bool m_isClearingEntry = false;

		public WorkItemPicker()
		{
			InitializeComponent();
		}

		public WorkItemPicker(WorkItemStore store, WorkItem workItem)
			: this()
		{
			m_workItem = workItem;
			m_pickControl = new PickWorkItemsControl(store, false);

			m_pickControl.Dock = DockStyle.Fill;
			m_pickControl.Location = new Point(0, 0);
			m_pickControl.Margin = new Padding(0, 0, 0, 0);
			m_pickControl.Name = "m_pickControl";
			m_pickControl.Size = new Size(526, 342);
			m_pickControl.PortfolioDisplayName = workItem.Project.Name;
			m_pickControl.FilterItemsNotInProject = true;
			m_pickControl.PublicQueriesText = "Team Queries";
			m_pickControl.PrivateQueriesText = "My Queries";

			pickWorkItemsControl.Controls.Add(m_pickControl);
		}

		public int WorkItemId
		{
			get
			{
				ArrayList list = m_pickControl.SelectedWorkItems();
				if (m_isClearingEntry == false && list != null && list.Count > 0 && list[0] != null)
				{
					return ((WorkItem)list[0]).Id;
				}
				return 0;
			}
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			int workItemId = this.WorkItemId;
			if (workItemId == 0 || (workItemId != m_workItem.Id && GetChildWorkItems(m_workItem, null).Contains(workItemId) == false))
			{
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			else
			{
				MessageBox.Show("Select a different work item to avoid creating a circular reference.", "Circular Reference", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}

		private List<int> GetChildWorkItems(WorkItem parentWorkItem, List<int> workItemList)
		{
			if (workItemList == null)
			{
				workItemList = new List<int>();
			}

			WorkItemStore store = parentWorkItem.Store;
			WorkItemCollection items = store.Query("SELECT [System.Id], [System.WorkItemType], [System.State], [System.Title] FROM WorkItems WHERE [System.TeamProject] = '" + parentWorkItem.Project.Name + "' AND [" + c_parentWorkItemFieldName + "] = " + parentWorkItem.Id.ToString() + " ORDER BY [System.WorkItemType], [System.Id]");

			if (items.Count > 0)
			{
				foreach (WorkItem childWorkItem in items)
				{
					//Add Child to list
					workItemList.Add(childWorkItem.Id);

					//Repeat for all children of this child
					GetChildWorkItems(childWorkItem, workItemList);
				}
			}

			return workItemList;
		}

		private void buttonClear_Click(object sender, EventArgs e)
		{
			//clear entry
			m_isClearingEntry = true;
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
	}
}
